package com.love.marriage.userCommissionBalance.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userCommissionBalance.entity.UserCommissionBalance;
import com.love.marriage.userCommissionBalance.mapper.UserCommissionBalanceMapper;
import com.love.marriage.userCommissionBalance.service.UserCommissionBalanceService;
import com.love.marriage.userCommissionBalance.vo.UserCommissionBalanceVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户回佣余额表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-11-10 18:51:27
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserCommissionBalanceServiceImp extends ServiceImpl<UserCommissionBalanceMapper, UserCommissionBalance> implements UserCommissionBalanceService {

    private final UserCommissionBalanceMapper userCommissionBalanceMapper;

    @Override
    public Page<UserCommissionBalanceVo> getPage(Long pageNum, Long pageSize, UserCommissionBalanceVo userCommissionBalanceVo) {
        Page<UserCommissionBalanceVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userCommissionBalanceMapper.getPage(page, userCommissionBalanceVo);
    }

    @Override
    public UserCommissionBalanceVo queryUserCommissionBalance(Long id) {
        return userCommissionBalanceMapper.queryUserCommissionBalance(id);
    }

    @Override
    public UserCommissionBalanceVo queryByUserId(Long userId) {
        return userCommissionBalanceMapper.queryByUserId(userId);
    }

    @Override
    public UserCommissionBalanceVo queryTotalCommissionByUserId(Long userId) {
        return userCommissionBalanceMapper.queryTotalCommissionByUserId(userId);
    }

    @Override
    public UserCommissionBalanceVo addUserCommissionBalance(UserCommissionBalanceVo userCommissionBalanceVo) {
        UserCommissionBalance userCommissionBalance = new UserCommissionBalance();
        BeanUtils.copyProperties(userCommissionBalanceVo, userCommissionBalance);
        userCommissionBalanceMapper.insert(userCommissionBalance);
        UserCommissionBalanceVo res = new UserCommissionBalanceVo();
        BeanUtils.copyProperties(userCommissionBalance, res);
        return res;
    }

    @Override
    public UserCommissionBalanceVo updateUserCommissionBalance(UserCommissionBalanceVo userCommissionBalanceVo) {
        UserCommissionBalance userCommissionBalance = this.getById(userCommissionBalanceVo.getId());
        BeanUtils.copyProperties(userCommissionBalanceVo, userCommissionBalance, "id", "version");
        this.updateById(userCommissionBalance);
        return userCommissionBalanceVo;
    }

    @Override
    public Boolean deleteUserCommissionBalance(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userCommissionBalanceMapper.deleteBatchIds(idArr);
        return true;
    }
}
